﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using DataAccessObject;
using EntityObject;
using System.Xml.Linq;
using System.Data;
using System.Text;
using System.Threading;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.EO;

namespace SharedLibraries
{
    public class ScheduleJobs
    {
        /// <summary>
        /// Các công việc sau sẽ được tự động thực hiện sau 3 phút:
        ///     - Gọi hàm gửi mail khi có người đăng nhập, bình luận ở mục hỏi đáp.
        ///     - Gọi hàm kiểm tra thay đổi và tạo lại các file: sitemap.html, sitemap.xml, rss.xml.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public static void Job_Frequency_3m(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (!StringEx.Equals((System.Configuration.ConfigurationManager.AppSettings["isEnableJob"]), "1"))
                {
                    Jobs a = new Jobs();
                    a.Job_Update_Notification(null);
                    return;
                }
                TempEO objTemp = (new TempDAO()).SelectItem(Temp_PK_iTempID_C.gSYNC);
                if (objTemp != null)
                {
                    if (!StringEx.Equals(objTemp.sContent, CustomerCache.tblTemp_sContent_Change_DB))
                    {
                        ////Jobs.Job_Sent_Mail_Notification();
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Chart);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Notification);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Sitemap);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Rss);
                        ////ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Sitemap_Rss);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Last_Modify);

                        Jobs a = new Jobs();
                        a.Job_Update_Chart(null);
                        a.Job_Update_Notification(null);
                        //a.Job_Update_Sitemap(null);
                        //a.Job_Update_Rss(null);
                        a.Job_Update_Last_Modify(null);

                        Jobs.Job_Update_Sitemap_Rss();
                        //Commons.WriteLogError(Commons.NotifyBot_Sitemap_Change() + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));

                        #region "update change"
                        CustomerCache.tblTemp_sContent_Change_DB = objTemp.sContent;
                        #endregion
                    }
                }
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }


        /// <summary>
        /// Các công việc sau sẽ được tự động thực hiện sau 1 ngày:
        ///     - Gọi hàm gửi mail chúc mừng sinh nhật.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public static void Job_Frequency_1d(object source, System.Timers.ElapsedEventArgs e)
        {
        }


        /// <summary>
        /// Các công việc sau sẽ được tự động thực hiện sau 7 ngày (1 tuần):
        ///     - Gọi hàm gửi mail cho những người đăng ký nhận tin tức.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public static void Job_Frequency_7d(object source, System.Timers.ElapsedEventArgs e)
        {
        }
    }

    public class Jobs
    {
        public void Job_Sent_Mail_Notification(Object threadContext)
        {
            String sSubject = "";
            String sBody = "";
            String sPath = "";
            if (CustomerCache.lstEmailWaitSent.Count > 0)
            {
                foreach (var item in CustomerCache.lstEmailWaitSent)
                {
                    if (item.iStatus == Constants.Email_iStatus_C.WAIT)
                    {
                        switch (item.iType)
                        {
                            case Constants.Email_iType_C.Login: break;
                            case Constants.Email_iType_C.Notification_Comment:
                                sPath = CustomerCache.strUrlTempMail;
                                sSubject = "Thư thông báo có bình luận mới";
                                if (File.Exists(sPath))
                                    sBody = System.IO.File.ReadAllText(sPath);
                                sBody = sBody.Replace("[@sName]", item.sName);
                                sBody = sBody.Replace("[@sUrl1]", item.s1);
                                sBody = sBody.Replace("[@sUrl2]", item.s2);

                                break;
                        }
                        if (SharedLibraries.Commons.SendMail(item.sName, System.Configuration.ConfigurationManager.AppSettings["EMAIL"], item.sEmail, sSubject, sBody))
                            item.iStatus = Constants.Email_iStatus_C.OK;
                        else
                            item.iStatus = Constants.Email_iStatus_C.NOT_OK;
                    }
                }
            }
            sPath = CustomerCache.strUrlLogMail;
            if (!File.Exists(sPath))
                File.Create(sPath);
            using (StreamWriter newTask = new StreamWriter(sPath, false))
            {
                newTask.WriteLine(JsonEx.ListObject2Json(CustomerCache.lstEmailWaitSent));
                newTask.Close();
            }
        }

        public static void Job_Update_Sitemap_Rss()
        {
            try
            {
                String sNewContent;
                List<SEOEO> lstRss = new List<SEOEO>();
                lstRss = (List<SEOEO>)ConvertEx.ConvertDataTableToListObject<SEOEO>((new SEODAO()).SelectBy_Status(true), lstRss.GetType());
                TempEO _TempEO;

                #region "sitemap.html"
                sNewContent =
                    "<%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"SitemapUC.ascx.cs\" Inherits=\"ThangNM.UserControl.SitemapUC\" %>" +
                    "\n<div class=\"css-treeview\">" +
                    "\n<ul>" +
                        "\n\t<li>" +
                            "\n\t\t<h1>" +
                                "\n\t\t\t<input type=\"checkbox\" id=\"item\" checked=\"checked\">" +
                                "\n\t\t\t<label for=\"item\">" +
                                    "\n\t\t\t\t<a href=\"http://nguyenmanhthang.net\" title=\"Nguyễn Mạnh Thắng - Đơn giản là chia sẻ\" target=\"_blank\">Nguyễn Mạnh Thắng - Đơn giản là chia sẻ</a>" +
                                "\n\t\t\t</label>" +
                            "\n\t\t</h1>" +
                            "\n\t\t<ul>" +
                            "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-1\"><label for=\"item-1\">Danh mục bài viết</label>" +
                    "\n\t\t\t\t<ul>";

                var lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Category && s.PK_iSEO.ToString().StartsWith(ConstEx.TableID_C.Topic.ToString()));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-2\"><label for=\"item-2\">Bài viết chia sẻ</label>" +
                    "\n\t\t\t\t<ul>";

                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Topic);
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-3\"><label for=\"item-3\">Giải trí</label>" +
                    "\n\t\t\t\t<ul>" +
                    "\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-1\"><label for=\"item-3-1\">Chơi game</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Choi_Game + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-2\"><label for=\"item-3-2\">Tải game</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Tai_Game + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-3\"><label for=\"item-3-3\">Xem phim - TV online</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Xem_Phim_TV_Online + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-4\"><label for=\"item-3-4\">Nghe nhạc - Radio</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Nghe_Nhac_Radio + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-5\"><label for=\"item-3-5\">Sách - truyện</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Sach_Truyen + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-6\"><label for=\"item-3-6\">Hình ảnh động</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Hinh_Anh_Dong + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-3-7\"><label for=\"item-3-7\">Độc và lạ</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Doc_Va_La + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-4\"><label for=\"item-4\">Ứng dụng online</label>" +
                    "\n\t\t\t\t<ul>" +
                "\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-4-1\"><label for=\"item-4-1\">Công cụ</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Media && s.FK_sCategory.Equals("," + Constants.Media_iType_C.Ung_Dung + ","));
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
"\n\t\t\t\t\t<li>" +
                    "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"item-4-2\"><label for=\"item-4-2\">Chia sẻ file</label>" +
                    "\n\t\t\t\t\t\t<ul>";
                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.File);
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t\t\t</ul>" +
        "\n\t\t\t\t\t</li>" +
         "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/kiem-tra-trac-nghiem\" title=\"Kiểm tra trắc nghiệm\" class=\"leaf\" target=\"_blank\">Kiểm tra trắc nghiệm</a></li>" +
                                "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/\" title=\"Chat room\" class=\"leaf\" target=\"_blank\">Chat room</a></li>" +
                                "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/\" title=\"Photoshop\" class=\"leaf\" target=\"_blank\">Photoshop</a></li>" +
                                "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/\" title=\"Chuyển định dạng file\" class=\"leaf\" target=\"_blank\">Chuyển định dạng file</a></li>" +
                                "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/\" title=\"Quét virus\" class=\"leaf\" target=\"_blank\">Quét virus</a></li>" +
                                "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/\" title=\"Download driver\" class=\"leaf\" target=\"_blank\">Download driver</a></li>" +


                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-5\"><label for=\"item-5\">Hỏi đáp</label>" +
                    "\n\t\t\t\t<ul>";

                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.QuestionAnswer);
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                "\n\t\t\t<li>" +
                    "\n\t\t\t\t<input type=\"checkbox\" id=\"item-6\"><label for=\"item-6\">Tác giả</label>" +
                    "\n\t\t\t\t<ul>";

                lstRssByType = lstRss.FindAll(s => s.PK_iTable == ConstEx.TableID_C.Account);
                foreach (var item in lstRssByType)
                {
                    sNewContent = sNewContent + "\n\t\t\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\" class=\"leaf\" target=\"_blank\">" + item.sTitle + "</a></li>";
                }
                sNewContent = sNewContent +
                    "\n\t\t\t\t</ul>" +
                "\n\t\t\t</li>" +
                                "\n\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/lien-he.html\" title=\"Liên hệ\" class=\"leaf\" target=\"_blank\">Liên hệ</a></li>" +
                                "\n\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/tim-kiem.html\" title=\"Tìm kiếm\" class=\"leaf\" target=\"_blank\">Tìm kiếm</a></li>" +
                                "\n\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/sitemap.html\" title=\"Bản đồ website\" class=\"leaf\" target=\"_blank\">Bản đồ website</a></li>" +
                                "\n\t\t\t<li><i class=\"fa fa-file-text-o leaf\"></i> <a href=\"http://nguyenmanhthang.net/rss.xml\" title=\"Rss Feed\" class=\"leaf\" target=\"_blank\">Rss Feed</a></li>" +
                            "\n\t\t</ul>" +
                        "\n\t</li>" +
                        "\n</ul>" +
                    "\n</div>";

                System.IO.File.WriteAllText(@"" + System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "GlobalResources\\SourceCode\\SitemapUC.ascx", sNewContent, Encoding.UTF8);
                _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.SITEMAP_HTML;
                _TempEO.sContent = sNewContent;
                (new TempDAO()).Update(_TempEO);
                #endregion

                #region "sitemap.xml"
                try
                {
                    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                    XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";

                    //XDocument Start
                    XDocument xDoc = new XDocument(
                        new XDeclaration("1.0", "UTF-8", "no"),
                        new XElement(ns + "urlset",
                        new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
                        new XAttribute(xsiNs + "schemaLocation",
                            "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),

                            from item in lstRss
                            select new XElement(ns + "url",
                                new XElement(ns + "loc", OtherEx.AddDomainInUrl(item.sUrlRewrite)),
                                new XElement(ns + "lastmod", item.tDateTimeModify.ToString(FormatEx.Date_Time.Format_DateTimeYYYYMMDD)),
                                new XElement(ns + "changefreq", GetListConstants.getValueKeyInSortedList(GetListConstants.Changefreq_GLC(), item.iChangefreq, false)),
                                new XElement(ns + "priority", item.fPriority)
                            )
                    ));
                    //XDocument End
                    sNewContent = xDoc.ToString();
                    System.IO.File.WriteAllText(@"" + System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "GlobalResources\\SourceCode\\sitemap.xml", sNewContent, Encoding.UTF8);
                    _TempEO = new TempEO();
                    _TempEO.PK_iTempID = Temp_PK_iTempID_C.SITEMAP_XML;
                    _TempEO.sContent = sNewContent;
                    (new TempDAO()).Update(_TempEO);
                }
                catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
                #endregion

                #region "rss.xml"
                try
                {
                    //XDocument Start
                    XDocument xDoc = new XDocument(
                        new XDeclaration("1.0", "UTF-8", "no"),
                        new XElement("rss",
                        new XAttribute(XNamespace.None + "version", "2.0"),
                        new XElement("channel",
                            new XElement("title", System.Configuration.ConfigurationManager.AppSettings["SLOGAN"].Substring(3)),
                            new XElement("atom-link",
                            new XAttribute(XNamespace.Xmlns + "atom", "http://www.w3.org/2005/Atom"),
                            new XAttribute("href", "http://nguyenmanhthang.net/rss.xml"),
                            new XAttribute("rel", "self"),
                            new XAttribute("type", "application/rss+xml")),
                            new XElement("link", System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"]),
                            new XElement("description", XmlEx.ReplaceSpecialXMLCharater(StringEx.TrimEx("Cung cấp các thủ thuật và phần mềm chất lượng cao, cùng với các Ebook và phần mềm hỗ trợ cho việc học CNTT. Ở đây cũng là nơi để các bạn giải trí với những bộ phim và những trò chơi hay. Mỗi bài viết được xuất bản mang theo một hi vọng là mang đến cho bạn đọc những thông tin hữu ích và thiết thực nhất"))),
                            new XElement("lastBuildDate", DateTime.Now.ToString("R")),
                            new XElement("language", "vi-VN"),
                            new XElement("generator", System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"]),
                            new XElement("copyright", System.Configuration.ConfigurationManager.AppSettings["COPYRIGHT"]),
                            from item in lstRss
                            select new XElement("item",
                                new XElement("title", XmlEx.ReplaceSpecialXMLCharater(StringEx.TrimEx(item.sTitle))),
                                new XElement("link", OtherEx.AddDomainInUrl(item.sUrlRewrite)),
                                new XElement("comments", OtherEx.AddDomainInUrl(item.sUrlRewrite) + "#comment"),
                                new XElement("pubDate", item.tDateTimeCreate.ToString("R")),
                                new XElement("guid", OtherEx.AddDomainInUrl(item.sUrlShort), new XAttribute("isPermaLink", "false")),
                                new XElement("description", XmlEx.ReplaceSpecialXMLCharater(StringEx.TrimEx(item.sDescription)))

                            )
                    )));
                    //XDocument End
                    sNewContent = xDoc.ToString();
                    sNewContent = sNewContent.Replace("atom-link", "atom:link");
                    sNewContent = sNewContent.Replace(" GMT</lastBuildDate>", " +0700</lastBuildDate>");
                    sNewContent = sNewContent.Replace(" GMT</pubDate>", " +0700</pubDate>");
                    System.IO.File.WriteAllText(@"" + System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "GlobalResources\\SourceCode\\rss.xml", sNewContent, Encoding.UTF8);
                    _TempEO = new TempEO();
                    _TempEO.PK_iTempID = Temp_PK_iTempID_C.RSS;
                    _TempEO.sContent = sNewContent;
                    (new TempDAO()).Update(_TempEO);
                }
                catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }// System.IO.File.WriteAllText(@"" + System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "rss.xml", ex.Message); }
                #endregion

                #region "MenuToolUC.ascx"
                List<DictionaryEO> lstDic = CategoryEx.Media_iTool();
                List<MediaEO> lstTool = new List<MediaEO>();
                lstTool = (List<MediaEO>)ConvertEx.ConvertDataTableToListObject<MediaEO>((new MediaDAO()).SelectBy_All_Tool(), lstTool.GetType());
                var query = from m in lstTool
                            join c in lstDic on Convert.ToString(m.iTool) equals Convert.ToString(c.Key)
                            select new { sUrlRewrite = m.sUrlRewrite, sTitle = m.sTitle, Group = c.Group };

                sNewContent =
                    "<%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"MenuToolUC.ascx.cs\" Inherits=\"ThangNM.UserControl.MenuToolUC\" %>" +
                    "\n<div class=\"grid_list\">" +
                    "\n\t<h3 class=\"font_18\"><i class=\"fa fa-puzzle-piece\"></i> Công cụ</h3>" +
                    "\n\t<ul id=\"tree1\">";

                var g1 = (from g in query where g.Group == Tool_Type_C.Group_String.ToString() select g).ToList();
                if (g1.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_String.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g1)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g2 = (from g in query where g.Group == Tool_Type_C.Group_Encode_Decode.ToString() select g).ToList();
                if (g2.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Encode_Decode.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g2)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g3 = (from g in query where g.Group == Tool_Type_C.Group_Generator.ToString() select g).ToList();
                if (g3.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Generator.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g3)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g4 = (from g in query where g.Group == Tool_Type_C.Group_Convert.ToString() select g).ToList();
                if (g4.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Convert.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g4)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g5 = (from g in query where g.Group == Tool_Type_C.Group_Color.ToString() select g).ToList();
                if (g5.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Color.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g5)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g6 = (from g in query where g.Group == Tool_Type_C.Group_Calculator.ToString() select g).ToList();
                if (g6.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Calculator.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g6)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                var g7 = (from g in query where g.Group == Tool_Type_C.Group_Other.ToString() select g).ToList();
                if (g7.Count > 0)
                {
                    sNewContent = sNewContent +
                        "\n\t\t<li>" + lstDic.Find(n => n.Key == Tool_Type_C.Group_Other.ToString()).Value +
                        "\n\t\t\t<ul>";
                    foreach (var item in g7)
                    {
                        sNewContent = sNewContent + "\n\t\t\t\t<li><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sTitle + "\">" + item.sTitle + "</a></li>";
                    }
                    sNewContent = sNewContent +
                        "\n\t\t\t</ul>" +
                        "\n\t\t</li>";
                }
                sNewContent = sNewContent +
                    "\n\t</ul>" +
                    "\n</div>";
                System.IO.File.WriteAllText(@"" + System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "GlobalResources\\SourceCode\\MenuToolUC.ascx", sNewContent, Encoding.UTF8);
                _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.MENU_TOOL;
                _TempEO.sContent = sNewContent;
                (new TempDAO()).Update(_TempEO);
                #endregion
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }

        public void Job_Update_Chart(Object threadContext)
        {
            try
            {
                TempEO _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.CHART;
                _TempEO.sContent = JsonEx.Object2Json((new TopicDAO()).getDataChart(99));
                (new TempDAO()).Update(_TempEO);
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }

        public void Job_Update_Notification(Object threadContext)
        {
            try
            {
                TempEO _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.NOTIFICATION;
                DataTable dt = (new TopicDAO()).getNotification();
                if (DataTableEx.DataTable_OK(dt))
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        switch (Convert.ToInt32(dr["PK_iTable"]))
                        {
                            case ConstEx.TableID_C.File:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-download fa-fw\"></i> " + dr["iTotal"] + " New File" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.Media:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-smile-o fa-fw\"></i> " + dr["iTotal"] + " New Media" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.QuestionAnswer:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-question fa-fw\"></i> " + dr["iTotal"] + " New Question" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.Answer:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-comment fa-fw\"></i> " + dr["iTotal"] + " New Answer" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.Quiz:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-trophy fa-fw\"></i> " + dr["iTotal"] + " New Quiz" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.Topic:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-copy fa-fw\"></i> " + dr["iTotal"] + " New Topic" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                            case ConstEx.TableID_C.Category:
                                _TempEO.sContent += "<li><a href=\"#\">" +
                                                            "<i class=\"fa fa-copy fa-fw\"></i> " + dr["iTotal"] + " New Category" +
                                                            "<span class=\"pull-right text-muted small\">" + dr["iLastChange"] + " minutes ago</span>" +
                                                    "</a></li>";
                                break;
                        }
                    }
                    TempEO objTemp = (new TempDAO()).SelectItem(Temp_PK_iTempID_C.tREQUEST);
                    if (objTemp != null)
                    {
                        _TempEO.sContent += "<li><a href=\"#\">" +
                                                "<i class=\"fa fa-refresh\"></i> Cập nhật vào lúc" +
                                                "<span class=\"pull-right text-muted small\">" + Convert.ToDateTime(objTemp.sContent).ToString(FormatEx.Date_Time.Format_DateTime) + " </span>" +
                                        "</a></li>";
                    }
                    Boolean isEnableJob = false;
                    if (StringEx.Equals((System.Configuration.ConfigurationManager.AppSettings["isEnableJob"]), "1"))
                        isEnableJob = true;

                    _TempEO.sContent += "<li><a href=\"#\">" +
                                            "<i class=\"fa fa-recycle\"></i> Trạng thái job" +
                                            "<span class=\"pull-right text-muted small\">" + (isEnableJob ? "Bật" : "Tắt") + " </span>" +
                                    "</a></li>";

                    (new TempDAO()).Update(_TempEO);

                }
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }

            //for (int i = 0; i < 100; i++)
            //{
            //    System.Diagnostics.Debug.WriteLine("#2: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            //}
        }

        public void Job_Update_Rss(Object threadContext)
        {
            try
            {
                TempEO _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.RSS;
                _TempEO.sContent = JsonEx.Object2Json((new TopicDAO()).SelectList_Rss(Int32.MaxValue));
                (new TempDAO()).Update(_TempEO);
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }

        public void Job_Update_Sitemap(Object threadContext)
        {
            try
            {
                TempEO _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.SITEMAP_XML;
                _TempEO.sContent = JsonEx.Object2Json((new SEODAO()).SelectBy_Status(true));
                (new TempDAO()).Update(_TempEO);
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }

        public void Job_Update_Last_Modify(Object threadContext)
        {
            try
            {
                TempEO _TempEO = new TempEO();
                _TempEO.PK_iTempID = Temp_PK_iTempID_C.tBUILD;
                _TempEO.sContent = DateTime.Now.ToString();
                (new TempDAO()).Update(_TempEO);
            }
            catch (Exception ex) { (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false); }
        }
    }

}